package com.wang.nowcoder.service;

import com.wang.nowcoder.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

/**
 * @author wang
 * @ClassName LikeService
 * @Description TODO
 * @date 2021/12/27 16:29
 * @Version 1.0
 */
@Service
@SuppressWarnings("all")
public class LikeService {
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     *点赞
     * @param userId
     * @param pojoType
     * @param pojoId
     */
    public void like(int userId,int pojoType,int pojoId,int pojoUserId){
//        String pojoLikeKey = RedisKeyUtil.getPojoLikeKey(pojoType, pojoId);
//        boolean member = redisTemplate.opsForSet().isMember(pojoLikeKey, userId);
//        if (member){
//            redisTemplate.opsForSet().remove(pojoLikeKey,userId);
//        }else {
//            redisTemplate.opsForSet().add(pojoLikeKey,userId);
//        }
        redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                String pojoLikeKey = RedisKeyUtil.getPojoLikeKey(pojoType, pojoId);
                String userLikeKey = RedisKeyUtil.getUserLikeKey(pojoUserId);

                boolean member = operations.opsForSet().isMember(pojoLikeKey,userId);
                operations.multi();

                if (member){
                    operations.opsForSet().remove(pojoLikeKey,userId);
                    operations.opsForValue().decrement(userLikeKey);
                }else {
                    operations.opsForSet().add(pojoLikeKey,userId);
                    operations.opsForValue().increment(userLikeKey);
                }

                return operations.exec();
            }
        });

    }

    /**
     * 查询某实体点赞数量
     * @param pojoType
     * @param pojoId
     * @return
     */
    public long selectPojoLikeCount(int pojoType,int pojoId){
        String pojoLikeKey = RedisKeyUtil.getPojoLikeKey(pojoType,pojoId);
        return redisTemplate.opsForSet().size(pojoLikeKey);
    }

    /**
     * 查询某人对实体的点赞状态
     * @param userId
     * @param pojoType
     * @param pojoId
     * @return
     */
    public int selectPojoLikeStatus(int userId,int pojoType,int pojoId){
        String pojoLikeKey = RedisKeyUtil.getPojoLikeKey(pojoType,pojoId);
        return redisTemplate.opsForSet().isMember(pojoLikeKey,userId) ? 1:0;
    }

    /**
     * 查询莫个用户获得的赞
     * @param userId
     * @return
     */
    public int selectUserLikeCount(int userId){
        String userLikeKey = RedisKeyUtil.getUserLikeKey(userId);
        Integer count = (Integer) redisTemplate.opsForValue().get(userLikeKey);
        return count == null?0:count.intValue();
    }
}
